package com.iafme.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.SearchCourseReqDTO;
import com.iafme.dto.res.AdmissionTicketResDTO;
import com.iafme.dto.res.PdfResDTO;
import com.iafme.dto.res.SearchCourseResDTO;
import com.iafme.freemarker.TemplateConfiguration;
import com.iafme.properties.ScanPayReq;
import com.iafme.dto.req.OrderReqDTO;
import com.iafme.dto.res.OrderResDTO;
import com.iafme.service.IOrderService;
import com.systemframework.constants.Constants;
import com.systemframework.sys.dto.request.common.PageResponseDTO;
import com.systemframework.util.SearchUtl;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/10 15:12
 */
@RestController
@RequestMapping(value = "/order")
@AllArgsConstructor
public class OrderController {

    private final IOrderService orderService;

    private final BeanSearcher beanSearcher;

    /**
     * 考试申请订单列表
     * @return
     */
    @PostMapping(value = "/lists")
    public Page<OrderResDTO> lists(@RequestBody PageResponseDTO pageResponseDTO){
        Map<String, Object> build = MapUtils.builder().page(pageResponseDTO.getPageNum()-1, pageResponseDTO.getPageSize())
                .field(OrderResDTO::getMemberId,StpUtil.getLoginIdAsInt()).op(Equal.class).build();
       return SearchUtl.searchDataList(pageResponseDTO.getPageNum(),pageResponseDTO.getPageSize(),beanSearcher.search(OrderResDTO.class, build));

    }

    /**
     * 取消订单
     * @param orderReqDTO
     * @return
     */
    @PostMapping(value = "/cancelOrder")
    public String cancelOrder(@RequestBody OrderReqDTO orderReqDTO){
        orderService.updateStatus(orderReqDTO);
        return Constants.SUCESS;
    }

    /**
     * 订单支付
     * @param orderReqDTO
     * @return
     */
    @PostMapping(value = "/payOrder")
    public ScanPayReq payOrder(@RequestBody OrderReqDTO orderReqDTO, HttpServletRequest request){

        return orderService.payOrder(orderReqDTO,request);
    }





    private final TemplateConfiguration templateConfiguration;

    /**
     * 查看准考证Pdf
     * @param orderId
     * @return
     */
    @GetMapping(value = "/showAdmissionNum/{orderId}")
    public PdfResDTO showAdmissionNum(@PathVariable Integer orderId){

        return templateConfiguration.sendPdF(orderId);
    }



    /**
     * 打印准考证
     */
    @PostMapping(value = "/admissionTicketList")
   public List<AdmissionTicketResDTO> admissionTicketList(@RequestBody OrderReqDTO orderReqDTO){
        Map<String, Object> build = MapUtils.builder().field(AdmissionTicketResDTO::getProjectId, orderReqDTO.getProjectId()).op(Equal.class)
                .field(AdmissionTicketResDTO::getLevelId, orderReqDTO.getLevelId()).op(Equal.class)
                .field(AdmissionTicketResDTO::getAdmissionNum).sql("$1 IS NOT NULL")
                .field(AdmissionTicketResDTO::getShowingsId, orderReqDTO.getShowingsId()).op(Equal.class)
                .field(AdmissionTicketResDTO::getMemberId, StpUtil.getLoginIdAsInt()).op(Equal.class).build();
        return beanSearcher.searchAll(AdmissionTicketResDTO.class,build);
   }

   @PostMapping(value = "/searchCourseList")
   public List<SearchCourseResDTO> searchCourseList(@RequestBody SearchCourseReqDTO searchCourseReqDTO){
       Map<String, Object> build = MapUtils.builder().field(SearchCourseResDTO::getMemberName, searchCourseReqDTO.getName()).op(Equal.class)
               .field(SearchCourseResDTO::getIdCard, searchCourseReqDTO.getIdCard()).op(Equal.class)
               .field(SearchCourseResDTO::getStatus,Constants.ONE).op(Equal.class)
               .field(SearchCourseResDTO::getProjectId, searchCourseReqDTO.getProjectId()).op(Equal.class)
               .field(SearchCourseResDTO::getShowingsId, searchCourseReqDTO.getShowingsId()).op(Equal.class)
               .field(SearchCourseResDTO::getLevelId, searchCourseReqDTO.getLevelId()).op(Equal.class).build();

       return beanSearcher.searchAll(SearchCourseResDTO.class,build);
   }


}
